package com.h3c.nem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.nem.annotation.CheckRole;
import com.h3c.nem.dto.*;
import com.h3c.nem.entity.SdEmailTemplate;
import com.h3c.nem.entity.TbWorker;
import com.h3c.nem.enums.RoleType;
import com.h3c.nem.mapper.TbWorkerMapper;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ISdDeptService;
import com.h3c.nem.service.ISdEmailTemplateService;
import com.h3c.nem.service.ITbWorkerService;
import com.h3c.nem.utils.AppBusinessException;
import com.h3c.nem.utils.BusiEnumDefine;
import com.h3c.nem.utils.ReturnCodeData;
import com.h3c.nem.utils.ReturnDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: TbWorkerController
 *
 * @Author: l22886
 * @Date: 2022-01-19
 * Description: tb_worker controller
 * Version: 1.0
 */
@Api(tags = " TbWorker 接口")
@Slf4j
@RestController
@CrossOrigin
@RequestMapping(value = "/tbWorker", produces = "application/json;charset=utf-8")
public class TbWorkerController {

    @Resource
    private ITbWorkerService tbWorkerService;
    @Resource
    private ISdDeptService deptService;
    @Resource
    private CommService commService;
    @Resource
    private ISdEmailTemplateService emailTemplateService;
    @Resource
    private TbWorkerMapper tbWorkerMapper;

    /**
     * @title: queryFuzzyListForPage
     * @description: 列表分页模糊查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "TbWorkerDTO", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryFuzzyListForPage", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> queryFuzzyListForPage(@ModelAttribute TbWorkerSeaDTO paramsDTO,
                                                            @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                            @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
//            paramsDTO.setDeptIdSet(deptService.getDeptChildIds(paramsDTO.getDirectDeptId()));
            if (!StringUtils.isEmpty(paramsDTO.getDirectDeptId())) {
                paramsDTO.setDeptIdSet(deptService.getDeptChildIds(paramsDTO.getDirectDeptId()));
            }
            String loginUserRole = commService.getLoginUserRole();
            boolean adminAuth = commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_ADMIN);
            boolean bpAuth = commService.checkLoginUserBP();
            boolean conAuth = commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_DEPT_CONTACTOR);
            boolean manAuth = commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_DEPT_MANAGER);
            boolean gbbzAuth = commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_GBBZ);
            boolean manAuth2 = commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_DIRECT_MANAGER);

            if (adminAuth) {
            } else if (bpAuth || conAuth || manAuth || gbbzAuth  || manAuth2) {
                Set<Integer> authDeptIds = deptService.getAuthDeptIds(bpAuth, false, manAuth, gbbzAuth);
//                commService.sendMessage();
                if (paramsDTO.getDeptIdSet() != null) {
                    paramsDTO.getDeptIdSet().retainAll(authDeptIds);
                } else {
                    paramsDTO.setDeptIdSet(authDeptIds);
                }
                if (manAuth2) paramsDTO.setWorkerManager(commService.getLoginUserId());
            } else {
                Page<TbPreWorkerDTO> page = PageHelper.startPage(currentPage, pageSize);
                PageInfo<TbPreWorkerDTO> pageInfo = page.toPageInfo();
                pageInfo.setList(new LinkedList<>());
                data.put("data", pageInfo);
                data.put("msg", "列表分页模糊查询成功");
                data.put("code", ReturnCodeData.SUCCESS);
            }
            if (adminAuth || gbbzAuth) {
                paramsDTO.setCanMore18(true);
            } else {
                paramsDTO.setCanMore18(false);
            }
            if (!StringUtils.isEmpty(paramsDTO.getWorkerStatus())) {
                paramsDTO.setWorkerStatuss(paramsDTO.getWorkerStatus().split(","));
            }
            if (!StringUtils.isEmpty(paramsDTO.getWorkerGradeString())) {
                paramsDTO.setWorkerGrades(paramsDTO.getWorkerGradeString().split(","));
            }
            if (!StringUtils.isEmpty(paramsDTO.getWorkerId())) {
                paramsDTO.setWorkerIds(paramsDTO.getWorkerId().split(","));
            }
            if (conAuth) {
                paramsDTO.setDeptContactor(commService.getLoginUserId());
            }
            //分页
            Page page = PageHelper.startPage(currentPage, pageSize);
            //执行
            List<TbWorkerDTO> resultDTOList = new ArrayList<>();
            if(commService.checkLoginUserOnlyRole(BusiEnumDefine.USER_ROLE_TEACHER)){
                List<TbWorker> tbWorkerList = tbWorkerMapper.selectList(new QueryWrapper<TbWorker>()
                        .like("teacher_info",commService.getLoginUserId()));
                resultDTOList = JSONArray.parseArray(JSONArray.toJSONString(tbWorkerList),TbWorkerDTO.class);
            }else {
                resultDTOList = tbWorkerService.selectFuzzyByDto(paramsDTO);
            }

            PageInfo<TbWorkerDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(resultDTOList);
            data.put("data", pageInfo);
            data.put("msg", "列表分页模糊查询成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行列表分页模糊查询异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * @title: queryFuzzyListForPage
     * @description: 查询辅导记录
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-02-14
     * @version: 1.0
     * @author: j26296
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "TbWorkerDTO", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryHelpRecodData", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> queryHelpRecodData(@ModelAttribute TbWorkerDTO paramsDTO,
                                                         @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                         @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //分页
            Page page = PageHelper.startPage(currentPage, pageSize);
            //执行
            List<TbWorkerDTO> resultDTOList = tbWorkerService.selectHelpRecord(paramsDTO);
            PageInfo<TbWorkerDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(resultDTOList);
            data.put("data", pageInfo);
            data.put("msg", "列表分页模糊查询成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("执行列表分页模糊查询异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }


    /**
     * @title: save
     * @description: 数据插入
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据插入", notes = "数据插入")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "TbWorker", paramType = "body")})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> save(@RequestBody TbWorkerDTO paramsDTO) {
        log.info("准备执行 数据插入，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //执行
            Integer result = tbWorkerService.insertDto(paramsDTO);
            data.put("data", result);
            data.put("msg", "数据插入成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行 数据插入 异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行 数据插入完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * @title: update
     * @description: 数据修改
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据修改", notes = "数据修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "TbWorker", paramType = "body")})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> update(@RequestBody TbWorkerDTO paramsDTO) {
        log.info("准备执行 数据修改，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //执行
            Integer result = tbWorkerService.updateDto(paramsDTO);
            data.put("data", result);
            data.put("msg", "数据修改成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行 数据修改 异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", e.toString());
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行 数据修改完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * @title: delete
     * @description: 数据删除
     * @params: [ id ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-01-19
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据删除", notes = "数据删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "TbWorker", paramType = "body")})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> delete(@RequestBody TbWorkerDTO paramsDTO) {
        log.info("准备执行 数据删除，传入的参数为：" + JSON.toJSONString(paramsDTO));
        JSONObject data = new JSONObject();
        try {
            //执行
            Integer result = tbWorkerService.deleteDto(paramsDTO);
            data.put("data", result);
            data.put("msg", "数据删除成功");
            data.put("code", ReturnCodeData.SUCCESS);
        } catch (Exception e) {
            log.error("执行 数据删除 异常，异常信息为：" + JSON.toJSONString(e));
            data.put("msg", "数据删除失败");
            data.put("code", ReturnCodeData.FAIL);
        }
        log.info("执行 数据删除 完成，准备返回的结果为：" + JSON.toJSONString(data));
        return new ResponseEntity<>(data, HttpStatus.ACCEPTED);
    }

    /**
     * @Title: getBasicInfo
     * @Description: 查询新员工基本信息
     * @Params: [com.h3c.nem.dto.TbWorkerDTO]
     * @Return: com.eos.common.util.Result
     * @Author: f23104
     * @Time: 2022/2/9 15:10
     */
    @ApiOperation(value = "查询新员工基本信息")
    @PostMapping(value = "/getBasicInfo")
    public Result getBasicInfo(@RequestBody TbWorkerDTO dto) {
        TbWorkerDTO basicInfo = tbWorkerService.getBasicInfo(dto);
        return new Result(true, StatusCodeEnum.SEARCH_SUCCESS, basicInfo);
    }

    @ApiOperation(value = "获取员工页面节点")
    @PostMapping(value = "/getWorkerFlowNodes")
    public Result getWorkerFlowNodes(@RequestBody TbWorkerDTO dto) {
        TbInstNodeDTO nodeDTO = tbWorkerService.getWorkerFlowNodes(dto);
        return new Result(true, StatusCodeEnum.SEARCH_SUCCESS, nodeDTO);
    }

//    @ApiOperation(value = "获取移动审批业务数据")
//    @PostMapping(value = "/getWxWorkerData")
//    public Result getWxWorkerData(@RequestBody TbWorkerDTO dto) {
//        Map<String, Object> wxWorkerData = tbWorkerService.getWxWorkerData(dto);
//        return new Result(true, StatusCodeEnum.SEARCH_SUCCESS, wxWorkerData);
//    }

    /**
     * 启动考核
     *
     * @param workerIds
     * @return
     */
    @PostMapping("startAssessment")
    public ResponseEntity<JSONObject> startAssessment(@RequestBody List<String> workerIds) {
        log.info("启动员工考核 员工工号" + workerIds.toString());
//        try {
        tbWorkerService.startFirstMonthAssessment(workerIds);
       /* }catch (Exception e){
            log.error(e.getMessage());
            return new ReturnDataUtil().error("启动员工考核失败");
        }*/
        log.info("启动员工考核成功");
        return new ReturnDataUtil().success();
    }

    /**
     * 终止考核
     *
     * @param paramsDTOS
     * @return
     */
    @PostMapping("endAssessment")
    public ResponseEntity<JSONObject> endAssessment(@RequestBody List<TbWorkerDTO> paramsDTOS) {
        log.info("终止考核");
        tbWorkerService.endAssessment(paramsDTOS);
        log.info("终止考核成功");
        return new ReturnDataUtil().success();
    }

    @PostMapping("exportWorkerList")
    public ResponseEntity<JSONObject> exportWorkerList(@RequestBody TbWorkerSeaDTO paramsDTO) {
        try {
            if (!StringUtils.isEmpty(paramsDTO.getDirectDeptId())) {
                paramsDTO.setDeptIdSet(deptService.getDeptChildIds(paramsDTO.getDirectDeptId()));
            }
            String loginUserRole = commService.getLoginUserRole();
            boolean adminAuth = commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_ADMIN);
            boolean bpAuth = commService.checkLoginUserBP();
            boolean conAuth = commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_DEPT_CONTACTOR);
            boolean manAuth = commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_DEPT_MANAGER);
            boolean gbbzAuth = commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_GBBZ);
            if (adminAuth) {
            } else if (bpAuth || conAuth || manAuth || gbbzAuth) {
                Set<Integer> authDeptIds = deptService.getAuthDeptIds(bpAuth, false, manAuth, gbbzAuth);
//                commService.sendMessage();
                if (paramsDTO.getDeptIdSet() != null) {
                    paramsDTO.getDeptIdSet().retainAll(authDeptIds);
                } else {
                    paramsDTO.setDeptIdSet(authDeptIds);
                }
            } else {
                paramsDTO.setWorkerId("1");
            }
            if (adminAuth || gbbzAuth) {
                paramsDTO.setCanMore18(true);
            } else {
                paramsDTO.setCanMore18(false);
            }
            if (!StringUtils.isEmpty(paramsDTO.getWorkerStatus())) {
                paramsDTO.setWorkerStatuss(paramsDTO.getWorkerStatus().split(","));
            }
            if (!StringUtils.isEmpty(paramsDTO.getWorkerGradeString())) {
                paramsDTO.setWorkerGrades(paramsDTO.getWorkerGradeString().split(","));
            }
            if (!StringUtils.isEmpty(paramsDTO.getWorkerId())) {
                paramsDTO.setWorkerIds(paramsDTO.getWorkerId().split(","));
            }
            if (conAuth) {
                paramsDTO.setDeptContactor(commService.getLoginUserId());
            }
            File file = tbWorkerService.exportWorkerList(paramsDTO);

            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            fileInputStream.read(buffer);
            fileInputStream.close();
            Map<String, String> result = new HashMap<>();
            result.put("file", Base64Utils.encodeToString(buffer));
            result.put("filename", file.getName());
            log.info("导出成功");
            return new ReturnDataUtil().success(result);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("导出error:" + e.getMessage());
            throw new AppBusinessException("导出出错");
        }
    }

    /**
     * 首页汇总信息
     *
     * @return
     */
    @GetMapping("getHomePageChart")
    public ResponseEntity<JSONObject> getHomePageChart() {
        log.info("首页汇总信息查询 ");
        Map<String, Object> map = tbWorkerService.homePageChart();
        log.info("首页汇总信息查询成功");
        return new ReturnDataUtil().success(map);
    }

    /**
     * 导入修改新员工数据
     *
     * @param file
     * @return
     */
    @PostMapping("importWorkerList")
    public ResponseEntity<JSONObject> importWorkerList(MultipartFile file) {
        tbWorkerService.importWorkerList(file);
        return new ReturnDataUtil().success();
    }

    @GetMapping("getRegularTemplate")
    public ResponseEntity<JSONObject> getRegularTemplate() {
        SdEmailTemplate sendRegular = emailTemplateService.getById("SEND_REGULAR");
        return new ReturnDataUtil().success(sendRegular);
    }

    @PostMapping("sendRegularNotice")
    public ResponseEntity<JSONObject> sendRegularNotice(@RequestBody SdEmailTemplateDTO emailTemplateDTO) {
        tbWorkerService.sendRegularNotice(emailTemplateDTO);
        return new ReturnDataUtil().success();
    }


    /**
     * 获取卡证制卡人员信息--分页
     *
     * @Author: h24002
     * @Date: 2022/6/24
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN, RoleType.USER_ROLE_CARD_DEPT})
    @ApiOperation(value = "卡证制卡人员列表分页查询", notes = "卡证制卡人员列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "分页查询参数信息", required = true, dataType = "TbWorkerCardQueryDTO", paramType = "query")
    })
    @PostMapping("/getWorkerCardListForPage")
    public Result getWorkerCardListForPage(@RequestBody PageQueryDTO<TbWorkerCardQueryDTO> paramsDTO) {
        log.info("准备执行  卡证制卡人员列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        try {
            //分页
            Page page = PageHelper.startPage(paramsDTO.getPageNum(), paramsDTO.getPageSize());
            TbWorkerCardQueryDTO dto = paramsDTO.getCondition();
            if (paramsDTO.getCondition() == null) {
                dto = new TbWorkerCardQueryDTO();
            }
            //执行
            List<TbWorkerCardDTO> resultDTOList = tbWorkerService.getWorkerCardListByDto(dto);
            PageInfo<TbWorkerCardDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(resultDTOList);

            log.info("执行完成 卡证制卡人员列表分页模糊查询，准备返回的结果为：" + JSON.toJSONString(resultDTOList));
            return new Result(true, StatusCodeEnum.NORMAL, pageInfo);
        } catch (Exception ex) {
            log.error("执行异常 卡证制卡人员列表分页模糊查询，异常信息为：" , ex);
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), ex.getMessage());
        }
    }

    /**
     * 导出卡证制卡人员信息--Excel导出
     *
     * @Param: dto 查询参数（无分页）
     * @Param: templateCode 模板编码
     * @Param: downLoadFileName 要下载的文件名称
     * @Author: h24002
     * @Date: 2022/6/27
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN, RoleType.USER_ROLE_CARD_DEPT})
    @ApiOperation(value = "卡证制卡人员信息导出", notes = "卡证制卡人员信息导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "查询参数信息", required = true, dataType = "TbWorkerCardQueryDTO", paramType = "query")
    })
    @PostMapping("/exportWorkerCardList")
    public Result exportWorkerCardList(@RequestBody TbWorkerCardQueryDTO paramsDTO) {
        try {
            File file = tbWorkerService.exportWorkerCardListByDto(paramsDTO, "EXPORT_WORKERCARD_LIST", "制卡人员信息导出");
            FileInputStream fiStream = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            fiStream.read(buffer);
            fiStream.close();
            Map<String, String> result = new HashMap<>();
            result.put("file", Base64Utils.encodeToString(buffer));
            result.put("filename", file.getName());
            return new Result(true, StatusCodeEnum.NORMAL, result);
        } catch (Exception ex) {
            log.error("制卡人员信息导出 异常：" , ex);
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), ex.getMessage());
        } finally {
            log.info("制卡人员信息导出 结束：" + JSONObject.toJSONString(paramsDTO));
        }
    }


    /**
     * 获取工卡照片人员信息--分页
     *
     * @Author: h24002
     * @Date: 2022/6/24
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN, RoleType.USER_ROLE_CARD_DEPT})
    @ApiOperation(value = "工卡照片人员分页查询", notes = "工卡照片人员分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "分页查询参数信息", required = true, dataType = "TbWorkerCardQueryDTO", paramType = "query")
    })
    @PostMapping("/getWorkerCardImageListForPage")
    public Result getWorkerCardImageListForPage(@RequestBody PageQueryDTO<TbWorkerCardQueryDTO> paramsDTO) {
        log.info("准备执行  工卡照片人员分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        try {
            //分页
            String orderField = paramsDTO.getOrderField();
            if (!StringUtils.hasText(orderField)) {
                orderField = " plan_report_date ";
            }
            String orderType = paramsDTO.getOrderType();
            if (!StringUtils.hasText(orderType)) {
                orderType = " DESC ";
            }
            String orderBy = orderField.trim() + " " + orderType.trim();
            Page page = PageHelper.startPage(paramsDTO.getPageNum(), paramsDTO.getPageSize(), orderBy);
            TbWorkerCardQueryDTO dto = paramsDTO.getCondition();
            if (paramsDTO.getCondition() == null) {
                dto = new TbWorkerCardQueryDTO();
            }
            //执行
            List<TbWorkerCardImageDTO> resultDTOList = tbWorkerService.getWorkerCardImageListByDto(dto);
            PageInfo<TbWorkerCardImageDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(resultDTOList);

            log.info("执行完成 工卡照片人员分页模糊查询，准备返回的结果为：" + JSON.toJSONString(resultDTOList));
            return new Result(true, StatusCodeEnum.NORMAL, pageInfo);
        } catch (Exception ex) {
            log.error("执行异常 工卡照片人员分页模糊查询，异常信息为：" , ex);
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), ex.getMessage());
        }
    }


    /**
     * 导出人员工卡照片
     *
     * @Param: imgList 要导出的员工信息（需包含链接，sfs文件ID）
     * @Param: dto 查询参数（无分页）
     * @Author: h24002
     * @Date: 2022/6/27
     */
    @CheckRole(roleArray = {RoleType.ROLE_ADMIN, RoleType.USER_ROLE_CARD_DEPT})
    @ApiOperation(value = "导出人员工卡照片", notes = "导出人员工卡照片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "要导出的员工信息", required = true, dataType = "TbWorkerCardImageQueryDTO", paramType = "query")
    })
    @PostMapping("/exportWorkerCardImageArrayByDto")
    public Result exportWorkerCardImageArrayByDto(@RequestBody TbWorkerCardImageQueryDTO query) {
        try {
            log.info(String.format("导出人员工卡照片 开始：[%s]", JSONObject.toJSONString(query)));
            Map<String, Object> fileResult = tbWorkerService.exportWorkerCardImageArrayByDto(query.getImgList(), query.getParamsDTO());
            String msg = (String) fileResult.get("msg");
            File file = (File) fileResult.get("file");

            FileInputStream fiStream = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            fiStream.read(buffer);
            fiStream.close();
            Map<String, String> result = new HashMap<>();
            result.put("file", Base64Utils.encodeToString(buffer));
            result.put("filename", file.getName());
            result.put("msg", msg);

            return new Result(true, StatusCodeEnum.NORMAL, result);
        } catch (Exception ex) {
            log.error("导出人员工卡照片 异常：" , ex);
            return new Result(false, StatusCodeEnum.UNKNOW_ERROR.getCode(), ex.getMessage());
        } finally {
            log.info("导出人员工卡照片 结束");
        }
    }

}
